﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form8 : Form
    {
        public Form8()
        {
            InitializeComponent();
        }

        //https://blog.csdn.net/az44yao/article/details/102532337
        private void Form8_Load(object sender, EventArgs e)
        {
            gvBind();//取数据

            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.PaleTurquoise;      //奇數列顏色

            //先建立個 CheckBox 欄
            DataGridViewCheckBoxColumn cbCol = new DataGridViewCheckBoxColumn();
            cbCol.Width = 70;   //設定寬度
            cbCol.HeaderText = "　  全选";
            cbCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;   //置中
            dataGridView1.Columns.Insert(0, cbCol);

            #region 建立全选 CheckBox

            //建立個矩形，等下計算 CheckBox 嵌入 GridView 的位置
            Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);
            rect.X = rect.Location.X + rect.Width / 4 - 9;
            rect.Y = rect.Location.Y + (rect.Height / 2 - 9);

            CheckBox cbHeader = new CheckBox();
            cbHeader.Name = "checkboxHeader";
            cbHeader.Size = new Size(18, 18);
            cbHeader.Location = rect.Location;
            //全選要設定的事件
            cbHeader.CheckedChanged += new EventHandler(cbHeader_CheckedChanged);

            //將 CheckBox 加入到 dataGridView
            dataGridView1.Controls.Add(cbHeader);
 
            #endregion

        }

        /// <summary>
        /// 取数据
        /// </summary>
        private void gvBind()
        {
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("编号", typeof(int));
            dt1.Columns.Add("姓名", typeof(string));
            dt1.Columns.Add("年龄", typeof(int));
            dt1.Columns.Add("性别", typeof(string));

            dt1.Rows.Add(1, "刘备", 20, "男");
            dt1.Rows.Add(2, "关羽", 30, "男");
            dt1.Rows.Add(3, "张飞", 40, "女");
            dt1.Rows.Add(4, "曹操", 50, "女");
            dt1.Rows.Add(5, "黄忠", 60, "女");
            dt1.Rows.Add(6, "周瑜", 70, "男");

            dataGridView1.DataSource = dt1;

            // https://blog.csdn.net/ypoflyer/article/details/9301101
            // 设置 DataGridView1 的第2列整列单元格为只读 
            dataGridView1.Columns[1].ReadOnly = true;
        }

        private void cbHeader_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dr in dataGridView1.Rows)
                dr.Cells[0].Value = ((CheckBox)dataGridView1.Controls.Find("checkboxHeader", true)[0]).Checked;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sEmpList = string.Empty;

            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                if ((bool)this.dataGridView1.Rows[i].Cells[0].Value)
                {
                    sEmpList = sEmpList +"/"+ this.dataGridView1.Rows[i].Cells[2].Value.ToString();
                }
            }
            Console.WriteLine(sEmpList);
        }
    }
}
